﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports SdlDotNet.Input
Imports SdlDotNet.Audio
Imports System.Drawing

Public Class GameOver
    Implements IScreen

    Private Players() As Player
    Private NewGameButton As AnimatedSprite
    Private WinningPlayer As Player = Nothing
    Private AvatarPoint As New Point(30, 30)
    Private WinnerMessage As TextSprite

    Private GameWasATie As Boolean

    Public Event RequestedNewGame()
    Public Event RequestedQuit()

    Public Sub New(ByRef players() As Player)
        Me.Players = players
        GameWasATie = CheckIfGameWasATie()
        SetWinningPlayer()
        InitializeSprites()
    End Sub

    Private Sub SetWinningPlayer()
        For Each player As Player In Players
            If (player.CheckIfDead() = False) Then
                WinningPlayer = player
            End If
        Next
    End Sub

    Private Function CheckIfGameWasATie() As Boolean
        Dim numberOfPlayersAlive As Integer = 0

        For Each player As Player In Players
            If (player.CheckIfDead() = False) Then
                numberOfPlayersAlive += 1
            End If
        Next

        If (numberOfPlayersAlive = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub InitializeSprites()
        Dim newGameButtonSurfaces As New SurfaceCollection()
        newGameButtonSurfaces.Add(New Surface("Resources/images/menu/newgame.png"))
        newGameButtonSurfaces.Add(New Surface("Resources/images/menu/newgame2.png"))
        NewGameButton = New AnimatedSprite(newGameButtonSurfaces, New Point(580, 485))
        NewGameButton.Animate = False


        WinnerMessage = New TextSprite("", New SdlDotNet.Graphics.Font("resources\fonts\Tuffy.ttf", 16), Color.White, True, New Point(200, 30))
        If (GameWasATie) Then
            WinnerMessage.Text = "Tie game! Boo!"
        Else
            WinnerMessage.Text = "Player " & WinningPlayer.PlayerNumber & " won! Hooray!"
        End If
    End Sub

    Public Sub Draw(ByRef display As Surface) Implements IScreen.Draw
        display.Fill(Color.Black)
        If (GameWasATie) Then
        Else
            WinningPlayer.Avatar.DrawLarge(display, AvatarPoint)
        End If
        display.Blit(WinnerMessage)
        display.Blit(NewGameButton)
    End Sub

    Public Sub HandleKeyboardUp(ByVal sender As Object, ByVal e As KeyboardEventArgs) Implements IScreen.HandleKeyboardUp
        If (e.Key = Key.Escape) Then
            RaiseEvent RequestedQuit()
        End If
    End Sub

    Public Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Implements IScreen.HandleMouseButtonUp
        Dim MousePoint As New Point(e.X, e.Y)

        If (NewGameButton.IntersectsWith(MousePoint)) Then
            CommonSounds.Click.Play()
            RaiseEvent RequestedNewGame()
        End If
    End Sub

    Public Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs) Implements IScreen.HandleMouseMotion
        Dim MousePoint As New Point(e.X, e.Y)

        NewGameButton.Frame = 0

        If (NewGameButton.IntersectsWith(MousePoint)) Then
            NewGameButton.Frame = 1
        End If
    End Sub

    Public Sub Dispose() Implements IScreen.Dispose
        NewGameButton.Dispose()
        Me.Finalize()
    End Sub
End Class

